package com.huluobo.a2107mvpimdemo.login.view;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.huluobo.a2107mvpimdemo.main.MainActivity;
import com.huluobo.a2107mvpimdemo.R;
import com.huluobo.a2107mvpimdemo.base.BaseActivity;
import com.huluobo.a2107mvpimdemo.login.presenter.LoginContract;
import com.huluobo.a2107mvpimdemo.login.presenter.LoginPresenter;
import com.huluobo.a2107mvpimdemo.register.view.RegisterActivity;
import com.hyphenate.chat.EMClient;

/**
 * Created by LC on 2023/9/20.
 */
public class LoginActivity extends BaseActivity<LoginContract.View, LoginPresenter> implements LoginContract.View {
    private EditText username, password;
    private Button login_btn;

    private TextView toRegister;

    @Override
    protected void initData() {
        presenter = new LoginPresenter();
    }

    @Override
    protected void initView() {
        username = findViewById(R.id.login_username);
        password = findViewById(R.id.login_password);
        login_btn = findViewById(R.id.login_btn);

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.login(username.getText().toString(), password.getText().toString());
            }
        });

        toRegister = findViewById(R.id.login_to_register);
        toRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onUsernameError() {
        username.setError("用户名输入错误,请输入3-20位字符,首字符为英文");
    }

    @Override
    public void onPasswordError() {
        password.setError("密码输入错误,请输入3-20位数字");
    }

    @Override
    public void onStartLogin() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showProgressDialog("开始登录");
            }
        });
    }

    @Override
    public void onLoginSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(LoginActivity.this, "登录成功:" + EMClient.getInstance().getCurrentUser(),
                        Toast.LENGTH_SHORT).show();
                dismissProgressDialog();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onLoginFailed(String error) {
        Log.i("TagA", "登录失败:" + error);
        dismissProgressDialog();
    }
}
